import os

import requests


def delete_background(inimg, outimg):
    response = requests.post(
        'https://api.remove.bg/v1.0/removebg',
        files={'image_file': open(inimg, 'rb')},
        data={'size': 'auto'},
        headers={'X-Api-Key': 'ztGx2sNdAjyAZKLCqQMGC5hP'},
    )
    if response.status_code == requests.codes.ok:
        with open(outimg, 'wb') as out:
            out.write(response.content)
    else:
        print("Error:", response.status_code, response.text)


if __name__ == '__main__':
    for root, dirs, files in os.walk("picture"):
        for file in files:
            inimg = os.path.join(root, file)
            delete_background(inimg, inimg)
    print("完成")